import socket


def main():
    # 创建一个tcp套接字
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 绑定ip和端口
    # udp_socket.bind(('', 8080))
    tcp_socket.bind(('127.0.0.1', 8080))
    # 套接字制动变被动监听(并发数)
    tcp_socket.listen(128)
    while True:
        # 拆包
        client_socket, client_addr = tcp_socket.accept()
        print("client_socket", client_socket)
        print("client_addr", client_addr)

        # 服务器接收客户端发送的数据
        while True:
            recv_data = client_socket.recv(1024)
            print(recv_data.decode("utf-8"))
            # 判断服务器是否已经关闭
            # 如果client_socket堵塞了
            # 等待客户端发送数据
            # 客户端调用close
            if recv_data:
                # 服务器返回数据
                client_socket.send("哈哈哈,我接受到你的信息了".encode("utf-8"))
            else:
                break
        # 关闭客户端socket
        client_socket.close()
    # 关闭套接字
    tcp_socket.close()


# 程序入口
if __name__ == "__main__":
    main()
